<template>
  <div class="loginBackground" style="height: 100%">
    <el-form :rules="rules" ref="loginForm" v-loading="loading"
             element-loading-text="正在登录..."
             element-loading-spinner="el-icon-loading"
             element-loading-background="rgba(0, 0, 0, 0.8)"
             :model="loginForm"
             class="loginContainer">
      <h2 class="loginTitle">系统登录</h2>
      <el-form-item prop="username">
        <el-input size="normal" type="text" v-model="loginForm.username" auto-complete="off"
                  placeholder="请输入用户名/手机号"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input size="normal" type="password" v-model="loginForm.password" auto-complete="off"
                  placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input size="normal" type="text" v-model="loginForm.code" auto-complete="off" placeholder="点击图片更换验证码"
                  @keydown.enter.native="submitLogin" style="width: 250px"></el-input>
        <img :src="vcUrl" @click="updateVerifyCode" alt="点击更换" title="点击更换" style="cursor: pointer">
      </el-form-item>
      <el-checkbox size="normal" class="loginRemember" v-model="checked">记住我</el-checkbox>
      <div style="text-align: center">
        <el-button size="normal" type="primary" style="width: 30%;" @click="submitLogin">登录</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import CryptoJS from "crypto-js";

export default {
  name: "Login",
  data() {
    return {
      imgSrc: require('../assets/background.jpg'),
      key: '',
      loading: false,
      vcUrl: '/api/verifyCode?time=' + new Date().getTime(),
      loginForm: {
        username: '',
        password: '',
        code: ''
      },
      checked: false,
      rules: {
        username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}],
        // 控制是否需要输入验证码
        code: [{required: false, message: '请输入验证码', trigger: 'blur'}]
      }
    }
  },
  // 加载组件执行方法
  mounted: function () {
    this.initUser();
  },
  methods: {
    initUser() {
      if (window.localStorage.getItem("i1") && window.localStorage.getItem("i1") === 'true') {
        this.checked = true;
        this.getRequest('/getKey').then(resp => {
          if (resp && resp.status === 200) {
            this.key = resp.data;
            this.loginForm.username = CryptoJS.AES.decrypt(window.localStorage.getItem("i2"), this.key).toString(CryptoJS.enc.Utf8);
            this.loginForm.password = CryptoJS.AES.decrypt(window.localStorage.getItem("i3"), this.key).toString(CryptoJS.enc.Utf8);
          }
        });
      } else {
        this.loginForm.username = '';
        this.loginForm.password = '';
      }
    },
    updateVerifyCode() {
      this.vcUrl = '/api/verifyCode?time=' + new Date().getTime();
    },
    submitLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          // 调用后端登录接口
          this.postRequest('/user/doLogin', this.loginForm).then(resp => {
            this.loading = false;
            if (resp && resp.status === 200) {
              this.$store.commit('INIT_CURRENT_USER', resp.data);
              window.localStorage.setItem("user", JSON.stringify(resp.data));

              let path = this.$route.query.redirect;
              this.$router.replace((path === '/' || path === undefined) ? '/home' : path);
              if (this.checked) {
                this.getRequest('/getKey').then(resp => {
                  if (resp && resp.status === 200) {
                    this.key = resp.data;
                    window.localStorage.setItem("i1", this.checked);
                    window.localStorage.setItem("i2", CryptoJS.AES.encrypt(this.loginForm.username, this.key).toString());
                    window.localStorage.setItem("i3", CryptoJS.AES.encrypt(this.loginForm.password, this.key).toString());
                  }
                });
              }
            } else {
              // Message.error({message: "用户名或密码错误!"})
              this.vcUrl = '/api/verifyCode?time=' + new Date().getTime();
            }
          });


        } else {
          return false;
        }
      });
    }
  }
}
</script>

<style>
.loginContainer {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 15px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.loginTitle {
  margin: 15px auto 20px auto;
  text-align: center;
  color: #505458;
}

.loginRemember {
  text-align: left;
  margin: 0 0 15px 0;
  padding-left: 0;
}

.el-form-item__content {
  display: flex;
  align-items: center;
}

.loginBackground{
  background: url("../assets/background.jpg");
  position:fixed;
  width: 100%;
  background-size:100% 100%;
}

</style>
